package com.jackeysun.widget

import android.graphics.*
import android.graphics.drawable.Drawable


class CircleProgressDrawable(
    var progressColor: Int = Color.RED,
    var progressPaintWidth: Float = 10f,
    var startAngle: Int = 0,
    var solidLength: Int = 4,
    var dottedLength: Int = 4,
    var intervalLength: Int = 0,
    var isSolid: Boolean = true
    /*var backgroundPaintWidth: Float = 10f,
    var backgroundColor: Int = Color.GRAY*/

) : Drawable() {
    private var progressPaint = Paint()
    private var backgroundPaint = Paint()
    private var backgroundRectF = RectF()
    private var progress = 0
    private var max = 100
    private var mRadius: Float = 10f

    init {
        progressPaint.style = Paint.Style.STROKE
        backgroundPaint.isAntiAlias = true//抗锯齿
        progressPaint.isDither = true//防抖动
        progressPaint.color = progressColor
        progressPaint.strokeWidth = progressPaintWidth

        if (isSolid) {//实线
            progressPaint.strokeCap = Paint.Cap.ROUND
        } else {//虚线
            progressPaint.pathEffect = DashPathEffect(
                floatArrayOf(solidLength.toFloat(), dottedLength.toFloat()),
                intervalLength.toFloat()
            )
        }

//        backgroundPaint.style = Paint.Style.STROKE
//        backgroundPaint.isAntiAlias = true//抗锯齿
//        backgroundPaint.isDither = true//防抖动
//        backgroundPaint.color = backgroundColor
//        backgroundPaint.strokeWidth = backgroundPaintWidth
//        //绘制长度为4的实线后再绘制长度为4的空白区域，0位间隔
//        backgroundPaint.pathEffect = DashPathEffect(floatArrayOf(4f, 4f), 0f)
    }

    fun setProgress(
        progress: Int = 0,
        max: Int = 100,
    ) {
        this.progress = progress
        this.max = max
        invalidateSelf()
    }

    override fun onBoundsChange(bounds: Rect) {
        super.onBoundsChange(bounds)
        backgroundRectF = RectF(bounds)
        mRadius = backgroundRectF.height().coerceAtMost(backgroundRectF.width()).div(2)
    }

    override fun draw(canvas: Canvas) {

//        canvas.save()//保存当前状态
//        canvas.translate(backgroundPaintWidth.div(2), backgroundPaintWidth.div(2))//平移
//        canvas.drawCircle(
//            mRadius.minus(backgroundPaintWidth.div(2)),
//            mRadius.minus(backgroundPaintWidth.div(2)),
//            mRadius.minus(backgroundPaintWidth.div(2)),
//            backgroundPaint
//        )
//
//        canvas.restore()//恢复上一次状态
        canvas.translate(progressPaintWidth.div(2), progressPaintWidth.div(2))
        val sweepAngle: Float = progress.toFloat().div(max).times(360)
        canvas.drawArc(
            RectF(
                0f,
                0f,
                mRadius.times(2).minus(progressPaintWidth),
                mRadius.times(2).minus(progressPaintWidth)
            ),
            startAngle.toFloat(),
            sweepAngle,
            false,
            progressPaint
        )
    }

    override fun setAlpha(alpha: Int) {
        progressPaint.alpha = alpha
    }

    override fun getOpacity(): Int {
        return PixelFormat.TRANSLUCENT
    }

    override fun setColorFilter(colorFilter: ColorFilter?) {
        progressPaint.colorFilter = colorFilter
    }
}